package com.example.yblin.hello24.DataUtils;

import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * JsonUtil.java
 * Version:1.1
 */

public class JsonUtil {

    public static final String TAG = "JsonUtil";

    /**
     * 解析一层JSON字符串为MAP
     *
     * @param jsonStr
     * @return
     */

    public static Map<String, String> analyze(String jsonStr) {
        Map<String, String> jsonlist = new LinkedHashMap<>();
        JSONObject jsonObject;
        Log.i(TAG, "JSON原始字符串=>" + jsonStr);
        try {
            jsonObject = new JSONObject(jsonStr);
            jsonlist = analyze(jsonObject);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonlist;
    }

    public static Map<String, String> analyze(JSONObject jsonObject) {
        Map<String, String> jsonlist = new LinkedHashMap<>();
        try {
            Iterator keys = jsonObject.keys();
            while (keys.hasNext()) {
                String key = keys.next().toString();
                String value = jsonObject.getString(key);
                jsonlist.put(key, value);
                Log.i(TAG, "Json解析结果=>" + key + ':' + value);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonlist;
    }
}
